package com.liziba.trend.controller;

import com.liziba.trend.api.IndexData;
import com.liziba.trend.entity.Result;
import com.liziba.trend.entity.StatusCode;
import com.liziba.trend.service.IndexDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther LiZiBa
 * @date 2021/2/2 21:54
 * @description: 指数基金数据
 **/

@RestController
@RequestMapping("/indexData")
@CrossOrigin
public class IndexDataController {

    @Autowired
    private IndexDataService indexDataService;


    /**
     * 接口获取基金代码
     * @param code 基金代码
     * @return
     */
    @GetMapping("/fetch/{code}")
    public Result<List<IndexData>> fetch(@PathVariable(value = "code") String code) {
        List<IndexData> indexDataList = indexDataService.fetchIndexDataFromThirdPart(code);
        return new Result<List<IndexData>>(true, StatusCode.OK, "接口获取数据成功！", indexDataList);
    }



    /**
     * 刷新缓存&获取数据
     * @param code 基金代码
     * @return
     * @throws Exception
     */
    @GetMapping("/fresh/{code}")
    public Result<List<IndexData>> fresh(@PathVariable(value = "code") String code) throws Exception {
        List<IndexData> indexDataList = indexDataService.fresh(code);
        return new Result<List<IndexData>>(true, StatusCode.OK, "刷新成功！", indexDataList);
    }

    /**
     * 移除缓存
     * @param code 基金代码
     * @return
     * @throws Exception
     */
    @GetMapping("/remove/{code}")
    public Result remove(@PathVariable(value = "code") String code) throws Exception {
        indexDataService.remove(code);
        return new Result(true, StatusCode.OK, "移除缓存成功！", null);
    }

    /**
     * 缓存中获取数据
     * @param code 基金代码
     * @return
     * @throws Exception
     */
    @GetMapping("/get/{code}")
    public Result<List<IndexData>> get(@PathVariable(value = "code") String code) throws Exception {
        List<IndexData> indexDataList = indexDataService.get(code);
        return new Result<List<IndexData>>(true, StatusCode.OK, "缓存中获取数据成功！", indexDataList);
    }

}
